/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2021-10-10     Sherman       first version
 * 2021-11-03     Sherman       Add icu_sample
 */

#include <rtthread.h>
#include "hal_data.h"
#include <rtdevice.h>
#include "lora_as32.h"
#include <rtdbg.h>
#include "aliyun_mqtt.h"
#include "lora_json_msg_obj.h"
#include "lora_net_manager.h"

extern  uint8_t esp8266_init_flag;

void get_node_data_callback(uint16_t addr, uint8_t *data, uint8_t len)
{
    uint8_t type = data[0];
    char *json_str = NULL;
    struct th_msg *p_th_msg = NULL;
    struct light_msg *p_light_msg = NULL;
    struct lora_node_property *property;

    switch (type) {
    case TEMP_WITH_HUMID:
        p_th_msg = (struct th_msg *)rt_malloc(sizeof(struct th_msg));
        if (p_th_msg == NULL) {
            break;
        }
        property = &p_th_msg->base_property;
        rt_memset(p_th_msg, 0, sizeof(struct th_msg));

        property->addr = addr;
        property->wsn_type = LORA_NET;
        property->sensor_type = TEMP_WITH_HUMID;
        p_th_msg->temp_value = data[1];
        p_th_msg->humid_value = data[2];

        json_str = create_json_th_sensor_msg(p_th_msg);
        aliyun_publish(json_str);
        rt_free(p_th_msg);
        rt_free(json_str);

        break;

    case LIGHT:

        p_light_msg = (struct light_msg *)rt_malloc(sizeof(struct light_msg));
        if (p_light_msg == NULL) {
            break;
        }
        rt_memset(p_light_msg, 0, sizeof(struct light_msg));
        property = &p_light_msg->base_property;

        property->addr = addr;
        property->wsn_type = LORA_NET;
        property->sensor_type = LIGHT;
        p_light_msg->light_value = (data[1] << 8) | data[2];

        json_str = create_json_light_sensor_msg(p_light_msg);
        aliyun_publish(json_str);
        rt_free(p_light_msg);
        rt_free(json_str);
        break;

    default:
        break;

    }
}

void hal_entry(void)
{
    int ret = 0;
    uint8_t connect_aliyun_flag = 1;
    uint8_t buf[8] = {0};

    rt_device_t lora_dev = rt_device_find("lora_as32");
    if (lora_dev == RT_NULL) {
        LOG_E("can't find lora device");
        return;
    }

    ret = rt_device_init(lora_dev);
    if (ret != 0) {
        LOG_E("\nlora init failed\n");
    }

    rt_device_open(lora_dev, 0);
    rt_kprintf("\nWelcome to lora gateway!\n");

    rt_device_control(lora_dev, RESET, buf);
    rt_thread_mdelay(200);                      //wait for the lora as32 reset finish

    buf[0] = 0;
    buf[1] = 0;
    buf[2] = 16;
    rt_device_control(lora_dev, SET_ADDR, buf);

    //aliyun_mqtt_init();
    lora_net_manager_init(lora_dev, get_node_data_callback);

    while(1) {

        if (esp8266_init_flag && connect_aliyun_flag) {
            rt_thread_mdelay(5000);
            aliyun_mqtt_init();
            connect_aliyun_flag = 0;
        }
        rt_thread_mdelay(1000);
    }

}


